﻿/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#ifndef BURNDLGMCU_H
#define BURNDLGMCU_H
#include <QSettings>
#include "BurnDlg.h"
#include "ui_BurnDlgMcu.h"
#include "BurnCtrl/readhex.h"
#include "ParamSettingDlg.h"
#include "MultipleBurnDlg.h"

namespace BurnDlg {
using namespace MemoryClass;
class BurnDlgMcu : public BurnDlg {
    Q_OBJECT
public:
    explicit BurnDlgMcu(ChipType chipType, QWidget *pParent = nullptr);
    ~BurnDlgMcu() override;
public slots:
    void SlotChipModeCurrentIndexChanged(const QString &arg1);
    void SlotTransferMethodCurrentIndexChanged(const QString &arg1) const;
    void SlotCbnSetfocusComboCom() const;
    void SlotBnClickedButtonSelectfile() override;
    void SlotConnectBtnClicked();
    void SlotShowLog(QString logStr, bool isShowTime, QColor color) const;
    void SlotSetConnectStatus(bool isEnable);
    void SlotSetTips(const QString &title, const QString &text) const;
    void SlotChangeChipModeByChipId(QString &chipName, uint chipId);
    void SlotBnClickedButtonStartburn() override;
    void SlotSetProgress(double pos) const;
    void SlotInsertPlainText(const QString &editStr) override;
    void SlotBnClickedSelectTarget();
    void SlotBnClickedExport();
    void SlotBnClickedErase();
    void SlotEraseAllMemory();
    void SlotParamSetting();
    void SlotGetParameter();
    void SlotSetParameter();
    void SlotBurnFileFinish();
    void ShowInfoAreaUI();
    void SlotBurnFileFailed();
    void SlotExportFinish() const;
    void SlotExportFail() const;
    void SlotBnClickedMultipleBurn();
protected:
    void Init();
    bool IsFileExist(const QString &fileName) const;
    bool ReadConfigIni();
    void ShowQComboBox(QSettings *fd, QString group, QComboBox *qBox, int baseSystem) const;
    void IniChipModel();
    void InitConnect();
    int GetChipMode() const;
    bool IsFirstSeriesChip(int chipMode) const;
    bool IniReadMemory(QSettings *&settings, const QString &absConfigPath) const;
    bool ReadChipConfigIni();
    void GetDefaultInfo();
    void ChipModeChangeInit();
    void TransmissionModeInit();
    void BaudRateListInit();
    void BaudRateUSBListInit();
    void CANBaudRateListInit();
    void SPIBaudRateListInit();
    void I2CBaudRateListInit();
    void IniFrequencies();
    void IniDebugMode();
    void GetProtectionPara();
    void ShowHistoryConfig() const;
    void HideConfigComponent() const;
    void SetUIComboBoxDisplay(bool isEnable) const;
    void SetUIBaudRateUartDisplay(bool isEnable) const;
    void SetUIBaudRateUSBDisplay(bool isEnable) const;
    void SetUIFrequenciesDisplay(bool isEnable) const;
    void SetUIDebugModeDisplay(bool isEnable) const;
    void SetUICANBaudRateListDisplay(bool isEnable) const;
    void SetUII2CRateDisplay(bool isEnable) const;
    void SetUISPIRateDisplay(bool isEnable) const;
    int GetTransmissionMode() const;
    void AnalyseBinFile(QString filePath);
    void InitTableView() const;
    void InitBurnCtrl() override;
    bool SaveCurrentConfig(QString chipName) const;
    void SetMcuConfig2BurnCtrl();
    bool ChangeChipMode(QString changeChipName);
    void UpdateList() override;
    void UpdateList(BIN_INFO_ARRAY &info) const;
    bool StartHexToBin(QString &fileName, MemoryAddresses * const &addresses);
    bool GetFileInfo(QString &fileName);
    int ReadHexInfo(QString &fileName, MemoryAddresses *addresses) const;
    int HexToBin(QString &hexName, QString &binName, MemoryAddresses &addresses) const;
    bool CheckFlashAddressLegitimacy(DWORD address, DWORD size,
        DWORD baseAddress, DWORD maxSize) const;
    void SetEnableBtn(bool enable) const;
    void GetCmdInfoArray(QMap<int, BIN_INFO> &infoArray);
    void FillParamAndShow(const QMap<int, BIN_INFO> &info);
    void SlotMultipleBurnDlgClose();

private:
    void SetMcuConfigValue(McuConfig &mcuConfig) const;
    void SetMcuJtagSwdValue(McuJtagSwd &jtagSwd);
    void ActionOfMultipleBurnDlgAfterSetInfoArray();
    void ActionOfMultipleBurnDlgAfterSetSramAddrInfo();

private:
    Ui::BurnDlgMcu *ui;
    QSettings *configIniRead;
    QSettings *chipIniRead;
    QString loaderPath;
    ulong loaderAddress;
    QString targetFileJTAG;
    QString targetFileSWD;
    QString targetFileInfoJTAG;
    QString targetFileInfoSWD;
    ulong chipIDAddress;
    ulong chipIDSize;
    QString hexToBinPath;
    ulong sramStartAddress;
    ulong sramEndAddress;
    ulong targetAddress;
    ulong parameterStartAddress;
    ulong mainStartAddress;
    ulong mainSize;
    ulong uploadStartAddress;
    ulong uploadSize;
    ulong infoStartAddress;
    int systemNum;
    QAction *actAbout;
    QAction *actChangeChip;
    bool isEnter;
    ParamSettingDlg *paramDlg;
    bool isInfoPart;
    bool isMultipleBurnOpen;
    ulong eraseBaseNum;
    MultipleBurnDlg *multipleBurnDlg;
};

}
#endif // BURNDLGMCU_H
